/*
 * ChartZoomPanel.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   ChartZoomPanel.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   16 jan 2013
 */

#include <limits>
#include <wx/wx.h>
#include <wx/spinctrl.h>
#include <stdexcept>
#include <boost/lexical_cast.hpp>

#include "ChartZoomPanel.hpp"

namespace tltgen {

  BEGIN_EVENT_TABLE(ChartZoomPanel, wxPanel)
  EVT_SPINCTRL(ID_VMin, ChartZoomPanel::OnVMinSpinCtrlChanged)
  EVT_SPINCTRL(ID_VMax, ChartZoomPanel::OnVMaxSpinCtrlChanged)
  EVT_SPINCTRL(ID_HMin, ChartZoomPanel::OnHMinSpinCtrlChanged)
  EVT_SPINCTRL(ID_HMax, ChartZoomPanel::OnHMaxSpinCtrlChanged)
  END_EVENT_TABLE()

  DEFINE_EVENT_TYPE(wxEVT_VERTICAL_START_CHANGED)
  DEFINE_EVENT_TYPE(wxEVT_VERTICAL_END_CHANGED)
  DEFINE_EVENT_TYPE(wxEVT_HORIZONTAL_START_CHANGED)
  DEFINE_EVENT_TYPE(wxEVT_HORIZONTAL_END_CHANGED)


  ChartZoomPanel::ChartZoomPanel(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name) :
  wxPanel(parent, id, pos, size, style, name)
  {
    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
    this->SetSizer(vbox);

    wxPanel *verticalAxisPanel = createVerticalAxisPanel();
    vbox->Add(verticalAxisPanel, 0, 0);

    wxPanel *horizontalAxisPanel = createHorizontalAxisPanel();
    vbox->Add(horizontalAxisPanel, 0, 0);
  }

  wxPanel *ChartZoomPanel::createVerticalAxisPanel()
  {
    const int maxValue = 100;
    const int minValue = 0;

    wxPanel *panel = new wxPanel(this, wxID_ANY);
    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
    panel->SetSizer(vbox);

    wxStaticText *stDescription = new wxStaticText(panel, wxID_ANY, _("Vertical range"));
    vbox->Add(stDescription, 0, 0);


    wxPanel *spinCtrlPanel = new wxPanel(panel, wxID_ANY);
    wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
    spinCtrlPanel->SetSizer(hbox);

    this->scVerticalMin = new wxSpinCtrl(spinCtrlPanel, ID_VMin, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, std::numeric_limits<int>::min(), maxValue - 1, minValue);
    hbox->Add(this->scVerticalMin, 0, wxEXPAND | wxALIGN_CENTER);

    wxStaticText *stSeparator = new wxStaticText(spinCtrlPanel, wxID_ANY, _(" - "));
    hbox->Add(stSeparator, 0, wxALIGN_CENTER);

    this->scVerticalMax = new wxSpinCtrl(spinCtrlPanel, ID_VMax, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, minValue + 1, std::numeric_limits<int>::max(), maxValue);
    hbox->Add(this->scVerticalMax, 0, wxEXPAND | wxALIGN_CENTER);

    vbox->Add(spinCtrlPanel, 0, 0);
    
    return panel;
  }

  wxPanel *ChartZoomPanel::createHorizontalAxisPanel()
  {
    const int maxValue = 100;
    const int minValue = 0;

    wxPanel *panel = new wxPanel(this, wxID_ANY);
    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
    panel->SetSizer(vbox);

    wxStaticText *stDescription = new wxStaticText(panel, wxID_ANY, _("Horizontal range"));
    vbox->Add(stDescription, 0, 0);


    wxPanel *spinCtrlPanel = new wxPanel(panel, wxID_ANY);
    wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
    spinCtrlPanel->SetSizer(hbox);

    this->scHorizontalMin = new wxSpinCtrl(spinCtrlPanel, ID_HMin, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, std::numeric_limits<int>::min(), maxValue - 1, minValue);
    hbox->Add(this->scHorizontalMin, 0, wxEXPAND | wxALIGN_CENTER);

    wxStaticText *stSeparator = new wxStaticText(spinCtrlPanel, wxID_ANY, _(" - "));
    hbox->Add(stSeparator, 0, wxALIGN_CENTER);

    this->scHorizontalMax = new wxSpinCtrl(spinCtrlPanel, ID_HMax, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, minValue + 1, std::numeric_limits<int>::max(), maxValue);
    hbox->Add(this->scHorizontalMax, 0, wxEXPAND | wxALIGN_CENTER);

    vbox->Add(spinCtrlPanel, 0, 0);
    
    return panel;
  }

  void ChartZoomPanel::OnVMinSpinCtrlChanged(wxSpinEvent& event)
  {
    this->scVerticalMax->SetRange(this->scVerticalMin->GetValue() + 1, this->scVerticalMax->GetMax());

    wxCommandEvent newEvent(wxEVT_VERTICAL_START_CHANGED, GetId());
    newEvent.SetEventObject(this);
    newEvent.SetInt(this->scVerticalMin->GetValue());
    GetEventHandler()->ProcessEvent(newEvent);
  }

  void ChartZoomPanel::OnVMaxSpinCtrlChanged(wxSpinEvent& event)
  {
    this->scVerticalMin->SetRange(this->scVerticalMin->GetMin(), this->scVerticalMax->GetValue() - 1);

    wxCommandEvent newEvent(wxEVT_VERTICAL_END_CHANGED, GetId());
    newEvent.SetEventObject(this);
    newEvent.SetInt(this->scVerticalMax->GetValue());
    GetEventHandler()->ProcessEvent(newEvent);
  }
  void ChartZoomPanel::OnHMinSpinCtrlChanged(wxSpinEvent& event)
  {
    this->scHorizontalMax->SetRange(this->scHorizontalMin->GetValue() + 1, this->scHorizontalMax->GetMax());

    wxCommandEvent newEvent(wxEVT_HORIZONTAL_START_CHANGED, GetId());
    newEvent.SetEventObject(this);
    newEvent.SetInt(this->scHorizontalMin->GetValue());
    GetEventHandler()->ProcessEvent(newEvent);
  }
  void ChartZoomPanel::OnHMaxSpinCtrlChanged(wxSpinEvent& event)
  {
    this->scHorizontalMin->SetRange(this->scHorizontalMin->GetMin(), this->scHorizontalMax->GetValue() - 1);

    wxCommandEvent newEvent(wxEVT_HORIZONTAL_END_CHANGED, GetId());
    newEvent.SetEventObject(this);
    newEvent.SetInt(this->scHorizontalMax->GetValue());
    GetEventHandler()->ProcessEvent(newEvent);
  }

  int ChartZoomPanel::getVerticalRangeMin() const
  {
    return this->scVerticalMin->GetMin();
  }
  int ChartZoomPanel::getVerticalRangeMax() const
  {
    return this->scVerticalMax->GetMax();
  }
  void ChartZoomPanel::setVerticalRangeBounds(int min, int max)
  {
    if (! isValidRange(min, max)) {
      throw std::invalid_argument("Values (" + boost::lexical_cast<std::string>(min) + "," + boost::lexical_cast<std::string>(max) + ") are not valid vertical range bounds.");
    }

    int minMax = this->scVerticalMax->GetValue() < min + 1 ? min + 1 : this->scVerticalMax->GetValue();
    this->scVerticalMin->SetRange(min, minMax);

    int maxMin = this->scVerticalMin->GetValue() > max - 1 ? max - 1 : this->scVerticalMin->GetValue();
    this->scVerticalMax->SetRange(maxMin, max);
  }

  bool ChartZoomPanel::isValidRange(int min, int max) const
  {
    return min < max;
  }

  int ChartZoomPanel::getVerticalRangeStart() const
  {
    return this->scVerticalMin->GetValue();
  }
  int ChartZoomPanel::getVerticalRangeEnd() const
  {
    return this->scVerticalMax->GetValue();
  }
  void ChartZoomPanel::setVerticalRange(int start, int end)
  {
    if (! isValidRange(start, end)) {
      //      throw std::invalid_argument("Values (" + boost::lexical_cast<std::string>(start) + "," + boost::lexical_cast<std::string>(end) + ") are not a valid vertical range.");
      end = start + 1;
    }

    this->scVerticalMin->SetRange(this->scVerticalMin->GetMin(), end-1);
    this->scVerticalMax->SetRange(start+1, this->scVerticalMax->GetMax());

    this->scVerticalMin->SetValue(start);
    this->scVerticalMax->SetValue(end);
  }


  int ChartZoomPanel::getHorizontalRangeMin() const
  {
    return this->scHorizontalMin->GetMin();
  }
  int ChartZoomPanel::getHorizontalRangeMax() const
  {
    return this->scHorizontalMax->GetMax();
  }
  void ChartZoomPanel::setHorizontalRangeBounds(int min, int max)
  {
    if (! isValidRange(min, max)) {
      throw std::invalid_argument("Values (" + boost::lexical_cast<std::string>(min) + "," + boost::lexical_cast<std::string>(max) + ") are not valid horizontal range bounds.");
    }

    int minMax = this->scHorizontalMax->GetValue() < min + 1 ? min + 1 : this->scHorizontalMax->GetValue();
    this->scHorizontalMin->SetRange(min, minMax);

    int maxMin = this->scHorizontalMin->GetValue() > max - 1 ? max - 1 : this->scHorizontalMin->GetValue();
    this->scHorizontalMax->SetRange(maxMin, max);
  }

  int ChartZoomPanel::getHorizontalRangeStart() const
  {
    return this->scHorizontalMin->GetValue();
  }
  int ChartZoomPanel::getHorizontalRangeEnd() const
  {
    return this->scHorizontalMax->GetValue();
  }
  void ChartZoomPanel::setHorizontalRange(int start, int end)
  {
    if (! isValidRange(start, end)) {
      throw std::invalid_argument("Values (" + boost::lexical_cast<std::string>(start) + "," + boost::lexical_cast<std::string>(end) + ") are not a valid horizontal range.");
    }

    this->scHorizontalMin->SetRange(this->scHorizontalMin->GetMin(), end-1);
    this->scHorizontalMax->SetRange(start+1, this->scHorizontalMax->GetMax());

    this->scHorizontalMin->SetValue(start);
    this->scHorizontalMax->SetValue(end);
  }


}
